package com.aier.user.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.classes.entity.ClassStudent;
import com.aier.classes.service.IClassService;
import com.aier.classes.service.IClassUserService;
import com.aier.common.common.SystemDateFormatUtil;
import com.aier.user.entity.CallRoll;
import com.aier.user.entity.CallRollClass;
import com.aier.user.entity.CallRollSchool;

@Scope("prototype")
@Component("userCallRollAction")
public class UserCallRollAction extends UserBaseAction {

    /**
     * 点名
     */
    private static final long serialVersionUID = 1L;
    @Resource(name = "classUserService")
    private IClassUserService classUserService;
    @Resource(name = "classService")
    private IClassService classService;
    
    private int s; // 错误码
    private Long cid; // 班级id
    private Integer sid; // 学校id
    private String month; // “yyyy-MM”
    
    private List<Map<String,Long>> crList;  //点名信息
    private List<CallRollClass> crcList;    // 班级点名统计信息
    private List<CallRollSchool> crsList;   // 学校点名统计信息
    private List<ClassStudent> csList; // 班级的全部学生
    private List<CallRoll> crl;  // 某天某班级的点名情况
    private boolean isGetParent; //是否获取学生家长
    
    

	@Override
    public String execute() {
        // 获取班级的学生列表
        csList = classUserService.searchAllClassStudentList(cid);
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 获取班级的学生列表
     * @return
     */
    public String searchCSList() {
        csList = classUserService.searchAllClassStudentList(cid);
        	for (ClassStudent cr : csList) {
        		cr.setCpList(classUserService.searchCPByCondition(cid, cr.getCsid()));
        	}
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 点名
     * @return
     */
    public String callRoll() {
        // s: 1成功  0已点过名
        if (crList == null) {
            crList = new ArrayList<Map<String,Long>>();
        }
        s = classService.insertCallRoll(cid, crList);
        Timestamp cdate = new Timestamp(new Date().getTime());
        String dateStr = SystemDateFormatUtil.formatDate(cdate, SystemDateFormatUtil.FORMAT_DATE);
        crl = classService.searchCRByDay(cid, dateStr);
        for (CallRoll cr : crl) {
            cr.setCpList(classUserService.searchCPByCondition(cid, cr.getCsid()));
        }
        
        return SUCCESS;
    }
    
    /**
     * 按天统计点名（班级教师）
     * @return
     */
    public String crcByDay() {
        
        crcList = classService.searchCRSByDay(cid, month);
        
        for (CallRollClass crc : crcList) {
            if (crc.getCount() > 0) {
                List<CallRoll> crl = classService.searchCRByDay(cid, crc.getCdate());
                for (CallRoll cr : crl) {
                    cr.setCpList(classUserService.searchCPByCondition(cid, cr.getCsid()));
                }
                crc.setCrList(crl);
            }
            
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 本月缺勤达人榜
     * @return
     */
    public String crcByMonth() {
        csList = classUserService.searchCSAbsentByMonth(cid, month);
        for (ClassStudent cs:csList) {
            cs.setCpList(classUserService.searchCPByCondition(cid, cs.getCsid()));
        }
        s = 1;  
        return SUCCESS;
    }
    
    /**
     * 按天统计点名（班级教师）以及本月缺勤达人榜（app端）
     * @return
     */
    public String crcByDayAndMonth() {
        
        crcList = classService.searchCRSByDay(cid, month);
        
        for (CallRollClass crs : crcList) {
            List<CallRoll> crl = classService.searchCRByDay(cid, crs.getCdate());
            for (CallRoll cr : crl) {
                cr.setCpList(classUserService.searchCPByCondition(cid, cr.getCsid()));
            }
            crs.setCrList(crl);
        }
        
        csList = classUserService.searchCSAbsentByMonth(cid, month);
        for (ClassStudent cs:csList) {
            cs.setCpList(classUserService.searchCPByCondition(cid, cs.getCsid()));
        }
        s = 1;
        
        return SUCCESS;
    }
    
    /**
     * 按天统计点名（所有班级，学校管理员）
     * @return
     */
    public String crsByDayAllClass() {
        
        crsList = classService.searchCRSByDayAllClass(sid, month);
        
        for (CallRollSchool crs : crsList) {
            if (crs.getCount() > 0) {
                List<CallRollClass> crcl = classService.searchCRCByDayAllClass(sid, crs.getCdate());
                for (CallRollClass crc : crcl) {
                    crc.setCrList(classService.searchCRByDay(crc.getCid(), crc.getCdate()));
                    crc.setCtList(classUserService.searchClassAdminTeacher(crc.getCid()));
                }
                crs.setCrcList(crcl);
            }
        }
        s = 1;
        
        return SUCCESS;
    }
    
    /**
     * 按月统计点名（所有班级，学校管理员）
     * @return
     */
    public String crsByMonthAllClass() {
        
        crsList = classService.searchCRSByDayAllClass(sid, month);
        
        crcList = classService.searchCRCByMonthAllClass(sid, month);
        for (CallRollClass crc : crcList) {
            if (crc.getMcount() > 0) {
                crc.setCrList(classService.searchCRByMonth(crc.getCid(), month));
            }
        }
        s = 1;
        return SUCCESS;
    }
    
    
    public int getS() {
        return s;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

    public void setClassUserService(IClassUserService classUserService) {
        this.classUserService = classUserService;
    }

    public List<ClassStudent> getCsList() {
        return csList;
    }

    public void setClassService(IClassService classService) {
        this.classService = classService;
    }

    public void setCrList(ArrayList<Map<String, Long>> crList) {
        this.crList = crList;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public List<CallRoll> getCrl() {
        return crl;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public List<CallRollClass> getCrcList() {
        return crcList;
    }

    public List<CallRollSchool> getCrsList() {
        return crsList;
    }
    public void setGetParent(boolean isGetParent) {
		this.isGetParent = isGetParent;
	}

}
